<?php
# test that date is less or equal than today date
function dateLTEToday($date){
$today = time();
$theDate = strtotime($date);

if ($theDate > $today)
	return 0;
return 1;
}

function verificationStep($retire_program_form){
foreach($retire_program_form as $form_key => $form_value)
{
	if( ! isset($retire_program_form[$form_key]['colors']) )
			$retire_program_form[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($retire_program_form[$form_key]['field_size']) )
			$retire_program_form[$form_key]['field_size'] = null;
	if( ! isset($retire_program_form[$form_key]['max_length']) )
			$retire_program_form[$form_key]['max_length'] = null;
	if( ! isset($retire_program_form[$form_key]['regex']) )
			$retire_program_form[$form_key]['colors'] = null;						  
	if( ! isset($retire_program_form[$form_key]['can_be_null']) )
			$retire_program_form[$form_key]['can_be_null'] =  false;
	
	if( ! isset($retire_program_form[$form_key]['readonly']) )
			$retire_program_form[$form_key]['readonly'] =  false;
	
	if( $retire_program_form[$form_key]['field_type'] == 'text')
	{
		if( isset($retire_program_form[$form_key]['initial_values']) )
			$retire_program_form[$form_key]['current_value'] = $retire_program_form[$form_key]['initial_values'];
		
		if ( !isset($retire_program_form[$form_key]['current_value']))
		$retire_program_form[$form_key]['current_value'] = '';
			
	}
	else if( $retire_program_form[$form_key]['field_type'] == 'drop_down_list')
	{
		$retire_program_form[$form_key]['current_value'] = "neselectat";
	}
	
	$retire_program_form[$form_key]['current_color'] = $retire_program_form[$form_key]['colors']['first'];	
}
return $retire_program_form;
}

# Generate form content based on provided data
function generate_form_content($array_data, $bReadOnly = 0 )
{
	if( !isset($array_data))
	{
		return "";
	}

	$get_form_content = "";
	$bFirst = 1;
									
	foreach($array_data as $form_field)
	{
		$change_row = 1;
		
		if(isset($form_field['same_row']))
			if ($form_field['same_row'] == "yes")
					$change_row = 0;
		
		if ( $bFirst )
		{
			$get_form_content .= "<tr>";
		}
		else
		{
			if( $change_row )
			{
				$get_form_content .= "</tr>";	
				$get_form_content .= "<tr>";
			}	
		}			
		
		$bFirst = 0;			
		
		if( isset($form_field['header']))
		{
			$get_form_content .= "</tr><tr><td>".$form_field['header']."</td></tr><tr>";
		}
			
		if( !isset($form_field['only_form_element']) && isset($form_field['field_display_name']) )
			
				$get_form_content .= "<td><b><font color=\"".$form_field['current_color']."\">" . $form_field['field_display_name'] . "</font></b></td>";
			
		
		if( $form_field['field_type'] == 'text' )
		{
			$get_form_content .= "<td><input type =\"text\" name =\"" . $form_field['post_name'] . "\"";
			$get_form_content .= " value=\"" . $form_field['current_value'] . "\"";
			//set the input id
			if (isset($form_field['id']))
				$get_form_content .= " id=\"".$form_field['id']."\"";
								
			if( 1 == $bReadOnly )
			{
				$get_form_content .= " readonly=\"readonly\"";
			}
					
			if (isset($form_field['title']))
				$get_form_content .= " title=\"".$form_field['title']."\"";
			
			if (isset($form_field['placeholder']))
				$get_form_content .= " placeholder=\"".$form_field['placeholder']."\"";
				
			$get_form_content .= " size=\"" . $form_field['field_size'] . "\"";
			$get_form_content .= " maxlength=\"" . $form_field['max_length'] . "\"";
			$get_form_content .= " ></td>";
		}
		else if( $form_field['field_type'] == 'drop_down_list' )
		{
			
			$id = "";
			if (isset($form_field['id']))
				$id = $form_field['id'] ;
				
			if( isset($form_field['initial_values']) )
					$drop_down_list_options = drop_down_list_generator($form_field['initial_values'],$form_field['current_value']);
					
			else
				$drop_down_list_options = drop_down_list_generator(null);	
				
				
			if( 1 == $bReadOnly )
			{	
				$get_form_content .= "<td><select name=\"".$form_field['post_name']."\" id=\"".$id."\" disabled=\"disabled\">";
			}
			else
			{
				$get_form_content .= "<td><select name=\"".$form_field['post_name']."\" id=\"".$id."\">";
			}
			
			$get_form_content .= $drop_down_list_options;
			$get_form_content .= "</select></td>";						
		}
		//create a hidden div
		else if( $form_field['field_type'] == 'div' ){
				$get_form_content .= "<div id=\"" .$form_field['id']. "\" style=\"display:none\"><span style=\"margin-top:30px;\" \">Un furnizor cu acest CUI/CF este deja inregistrat.</br>
				Introduceti un CUI/CF diferit!</span></div>";
				}
		else if ($form_field['field_type'] == 'span'){
			$get_form_content .= "<span id=\"" .$form_field['id']. "\" style=\"color:#68a;\"></span>";
				
				}
		
		
		if( isset( $form_field['extra_space_after'] ))
		{
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
			$get_form_content .="<td></td>";
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
		}
	}		
	$get_form_content .="</tr>";
	return $get_form_content;
}

# Function used to generate drop down list menu that sets and the id
function drop_down_list_generator($drop_down_list_options_array, $last_selected_value = null)
{ 
	$drop_down_list_options = "";				   

	if( null == $drop_down_list_options_array )
			return $drop_down_list_options;	
	
	# Default value used when drop-down list is displayed	
	$drop_down_list_options = $drop_down_list_options."<option value=\"neselectat\">...</option>";
					
	# foreach element in dropdown-list render options
	# if last_selected_value is different than null than select that value as being the default one displayed in interface 
	
	
	foreach($drop_down_list_options_array as $drop_down_list_element_key => $drop_down_list_element_value)
	{
		if ( $last_selected_value != null )
		{
			if( 0 == strcasecmp($last_selected_value,$drop_down_list_element_value) )
			{
				# if one of the options in drop-down list matches the provided value for last_selected_value make it the default one
				$drop_down_list_options = $drop_down_list_options."<option selected=\"selected\" value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			else
			{
				$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			
		}
		else
		{
			$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";
		}
	}
	return $drop_down_list_options;
}


function formConstraintsSatisfied(&$form_fields_with_speficications, &$erori){
		$ok = 1;
			foreach($form_fields_with_speficications as $form_key => $form_value)
			{
			if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
			{									
				if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
				|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
				//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
				{
					# Something went wrong with this form element so an error will be displayed to user
					$ok = 0;
					
					# Change color for element ; default will be red
					$form_fields_with_speficications[$form_key]['current_color'] = "red";
					if( isset($form_fields_with_speficications[$form_key]['colors'] ))
					{
						if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
						{
							//if (!isset($form_fields_with_speficications[$form_key]['can_be_null']))
								$form_fields_with_speficications[$form_key]['current_color'] = 
								$form_fields_with_speficications[$form_key]['colors']['second'];		
						}
					}
					
					
					
					# Generate an error if nothing it's provided
					$default_generated_error = "";
					
					if( $form_fields_with_speficications[$form_key]['field_type'] == "text")
					{
						$theField = $form_fields_with_speficications[$form_key];
						if ( !isset($theField ['can_be_null']) or 
							(isset($theField ['can_be_null']) and $theField['can_be_null'] != true))
							$default_generated_error = 
							"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
					{
						$default_generated_error =
						 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
					}
					else
					{
						$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
					}
					
					# Add the provided error or the default one
					if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
					{
						if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
						{
							$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
					
					continue;
				}
				
				# Check regex here
				if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
				{
					if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
					{
						if( preg_match($form_fields_with_speficications[$form_key]['regex'],
						$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']."sunt invalide</br>";
							}
							else
							{
								$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				
				# check validation functions
				if ( isset( $form_fields_with_speficications[$form_key]['validation_function'] ) )
				{
					if ( !empty( $form_fields_with_speficications[$form_key]['validation_function'] ) )
					{
						$function_name = $form_fields_with_speficications[$form_key]['validation_function'] ;
						$validation_value = $_POST[$form_fields_with_speficications[$form_key]['post_name']]; 
						
						if($function_name($validation_value) == 0 )
						{																					
							# Something went wrong with this form element so an error will be displayed to user
							$ok = 0;
							
							# Change color for element ; default will be red
							$form_fields_with_speficications[$form_key]['current_color'] = "red";
							if( isset($form_fields_with_speficications[$form_key]['colors'] ))
							{
								if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
								{
									$form_fields_with_speficications[$form_key]['current_color'] = 
									$form_fields_with_speficications[$form_key]['colors']['second'];		
								}
							}
							
							# Generate an error if nothing it's provided
							$default_generated_error = "";
							
							if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
							{
								$default_generated_error = 
								"Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']."
								 sunt invalide</br>";
							}
							else
							{
								$default_generated_error = 
								"Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
							}
							
							# Add the provided error or the default one
							if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
							{
								if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
								{
									$erori = 
										$erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
								}
								else
								{
									$erori = $erori.$default_generated_error;
								}
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						
							continue;
						}
					}
				}
				$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
			}
		}

	return $ok;
}

?>